package com.zhenwei.zscm.repo;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.OrderViewEntity;
import com.zhenwei.zscm.mapper.IOrderViewMapper;
import com.zhenwei.zscm.mappings.OrderViewMapping;
import com.zhenwei.zscm.model.dto.order.OrderViewDTO;
import com.zhenwei.zscm.model.vo.order.QueryOrderVO;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderViewRepo extends ServiceImpl<IOrderViewMapper, OrderViewEntity> {

    public List<OrderViewDTO> page(QueryOrderVO query) {
        var q = new LambdaQueryWrapper<OrderViewEntity>();
        // 工单号
        q.like(StrUtil.isNotBlank(query.getOrderNumber()), OrderViewEntity::getOrderNumber, query.getOrderNumber());
        // 客户
        q.eq(StrUtil.isNotBlank(query.getCustomerCode()), OrderViewEntity::getCustomerCode, query.getCustomerCode());
        // 工单状态
        q.eq(StrUtil.isNotBlank(query.getOrderStatus()), OrderViewEntity::getOrderStatus, query.getOrderStatus());
        // 产品
        q.eq(StrUtil.isNotBlank(query.getProductCode()), OrderViewEntity::getProductCode, query.getProductCode());
        // 联系人
        q.like(StrUtil.isNotBlank(query.getContactor()), OrderViewEntity::getContactor, query.getContactor());
        // 电话
        q.like(StrUtil.isNotBlank(query.getTelephone()), OrderViewEntity::getTelephone, query.getTelephone());
        // 产品sn
        q.like(StrUtil.isNotBlank(query.getProductSn()), OrderViewEntity::getProductSn, query.getProductSn());
        // 快递接收单号
        q.like(StrUtil.isNotBlank(query.getReceiveNumber()), OrderViewEntity::getReceiveNumber, query.getReceiveNumber());
        // 快递返回单号
        q.like(StrUtil.isNotBlank(query.getReturnNumber()), OrderViewEntity::getReturnNumber, query.getReturnNumber());
        // 支付状态
        q.eq(StrUtil.isNotBlank(query.getPayState()), OrderViewEntity::getPayState, query.getPayState());
        // 创建人/检测人/维修人
        q.and(StrUtil.isNotBlank(query.getCreateUserId()), wrapper->{
            wrapper.like(OrderViewEntity::getCUserId, query.getCreateUserId())
                    .or().like(OrderViewEntity::getCheckEngineerId, query.getCreateUserId())
                    .or().like(OrderViewEntity::getRepairEngineerId, query.getCreateUserId());
        });
        // 工单创建日期
        q.ge(ObjUtil.isNotNull(query.getOrderCreateDateStart()),OrderViewEntity::getCTime,query.getOrderCreateDateStart());
        q.le(ObjUtil.isNotNull(query.getOrderCreateDateEnd()),OrderViewEntity::getCTime,query.getOrderCreateDateEnd());
        // 快递接收日期
        q.ge(ObjUtil.isNotNull(query.getReceiveDateStart()),OrderViewEntity::getReceiveDate,query.getReceiveDateStart());
        q.le(ObjUtil.isNotNull(query.getReceiveDateEnd()),OrderViewEntity::getReceiveDate,query.getReceiveDateEnd());
        // 工单完成日期
        q.ge(ObjUtil.isNotNull(query.getOrderCompleteDateStart()),OrderViewEntity::getCompleteDate,query.getOrderCompleteDateStart());
        q.le(ObjUtil.isNotNull(query.getOrderCompleteDateEnd()),OrderViewEntity::getCompleteDate,query.getOrderCompleteDateEnd());

        q.select(
                OrderViewEntity::getGuid,
                OrderViewEntity::getOrderNumber,
                OrderViewEntity::getOrderStatus,
                OrderViewEntity::getContactor,
                OrderViewEntity::getTelephone,
                OrderViewEntity::getCUser,
                OrderViewEntity::getCheckEngineer,
                OrderViewEntity::getRepairEngineer,
                OrderViewEntity::getProductCount,
                OrderViewEntity::getCTime,
                OrderViewEntity::getCustomerName,
                OrderViewEntity::getOrderStatusName
        );
        q.groupBy(
                OrderViewEntity::getGuid,
                OrderViewEntity::getOrderNumber,
                OrderViewEntity::getOrderStatus,
                OrderViewEntity::getContactor,
                OrderViewEntity::getTelephone,
                OrderViewEntity::getCUser,
                OrderViewEntity::getCheckEngineer,
                OrderViewEntity::getRepairEngineer,
                OrderViewEntity::getProductCount,
                OrderViewEntity::getCTime,
                OrderViewEntity::getCustomerName,
                OrderViewEntity::getOrderStatusName
        );
        var p = new Page<OrderViewEntity>(query.getPageNo(), query.getPageSize(), true);
        Page<OrderViewEntity> page = this.page(p, q);
        query.setTotalCount(page.getTotal());
        var list = page.getRecords();
        return Linq.mapList(list, OrderViewMapping.INSTANCE::entity2Dto);
    }
}
